package 重温_Java并发基础.CompletableFuture案例;

import java.io.IOException;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @author idea
 * @data 2020/2/21
 */
public class MainDemo {


    private static Random rand = new Random();
    private static long t = System.currentTimeMillis();

    static int getMoreData() {
        System.out.println("begin to start compute");
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("end to start compute. passed " + (System.currentTimeMillis() - t) / 1000 + " seconds");

        return rand.nextInt(1000);
    }

    public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
        CompletableFuture<Integer> future=CompletableFuture.supplyAsync(MainDemo::getMoreData);
        Future<Integer> result=future.whenComplete((v,e)->{
            System.out.println(v);
            System.out.println(e);
        });
        System.out.println(result.get());
        System.in.read();
    }

}
